package kr.co.xosoft.activity;

import java.util.ArrayList;

import kr.co.xosoft.R;
import kr.co.xosoft.adapter.ServerChkSetupAdapter;
import kr.co.xosoft.common.Config;
import kr.co.xosoft.db.DBHandler;
import kr.co.xosoft.vo.ServerChkVO;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ServerChkSetupActivity extends Activity{
	private ListView lv01;
	private ListView lv02;
	Resources res;
	private DBHandler dbHandler;
	private ServerChkVO chkVO;
	private ArrayList<Object> infoList;
	private Dialog dialog;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.setup);
		Log.i("N1", "SetupActivity............start");

		lv01 = (ListView)findViewById(R.id.st_lv01);
		dbHandler = DBHandler.open(this);
		chkVO = dbHandler.select();
		res = getResources();
		
		setListAdapter(chkVO);
		
		lv01.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				Log.i("N1", "onItemClickListener......................... start");
				switch (position) {
				case 0:	
					returnDialog(position, ((TextView)view.findViewById(R.id.st_tv01)).getText().toString(), Config.viewType);
					break;
				case 1:	
					CheckBox cb = (CheckBox)view.findViewById(R.id.st_cb01);
					Log.i("N1", "cb.isSelected()........................."+cb.isSelected());
					if(cb.isChecked()){
						cb.setChecked(false);
						chkVO.setAutoflag(false);
					}else{
						cb.setChecked(true);
						chkVO.setAutoflag(true);
					}
					setListAdapter(chkVO);
					break;
				case 2:
					if(chkVO.isAutoflag()){
						returnDialog(position, ((TextView)view.findViewById(R.id.st_tv01)).getText().toString(), Config.waitTime);
					}
					Log.i("N1", "chkVO.isAutoflag()........................."+chkVO.isAutoflag());
					break;
				}
			}
		});
	}
	
	public void setListAdapter(ServerChkVO chkVO){
		infoList = new ArrayList<Object>();
		infoList.add(chkVO.getViewtype());
		infoList.add(chkVO.isAutoflag());
		infoList.add(chkVO.getWaittime());
		
		lv01.setAdapter(new ServerChkSetupAdapter(this, android.R.layout.simple_list_item_2, infoList, res));
	}
	
	public void returnDialog(int position, String title, String[] array){
		dialog = new Dialog(this);
		dialog.setContentView(R.layout.dialogview);
		dialog.setTitle(title);
		dialog.setCancelable(true);
		lv02 = (ListView)dialog.findViewById(R.id.dl_lv01);
		lv02.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_single_choice, array)); 
		lv02.setItemsCanFocus(false);
		lv02.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		int tempPosition = 0;
		if(position == 0){
			for(int i = 0; i < array.length; i++){
				if(chkVO.getViewtype() == Config.viewTypeValue[i]){
					tempPosition = i;
				}
			}
		}else{
			for(int i = 0; i < array.length; i++){
				if((int)(chkVO.getWaittime()/1000) == Config.waitTimeValue[i]){
					tempPosition = i;
				}
			}
		}
		lv02.setItemChecked(tempPosition, true);
		lv02.setOnItemClickListener(dialogListener(position));
		Log.i("N1", "tempPosition.. : "+tempPosition);
		dialog.show();
		
	}
	
	public OnItemClickListener dialogListener(int position){
		OnItemClickListener clickListener = null;
		switch (position) {
		case 0:
			clickListener = new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					chkVO.setViewtype(Config.viewTypeValue[arg2]);
					Log.i("N1", "OnItemClickListener.. position 0");
					dialog.cancel();
				}
			};
			break;
		case 2:
			clickListener = new OnItemClickListener() {
				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					chkVO.setWaittime(Config.waitTimeValue[arg2]);
					Log.i("N1", "OnItemClickListener.. position 2");
					dialog.cancel();
				}
			};
			break;
		}
		return clickListener;
	}
	
	@Override
	protected void onResume() {
		Log.i("N1", "Setup onResume");
		super.onResume();
	}
	
	@Override
	protected void onDestroy() {
		dbHandler.close();
		Log.i("N1", "Setup onDestroy");
		super.onDestroy();
	}
	
	@Override
	protected void onPause() {
		if(dbHandler.selectCount() > 0){
			dbHandler.updateAll("1", chkVO.isAutoflag(), (int)(chkVO.getWaittime()/1000), chkVO.getViewtype());
			Log.i("N1", "update..............................OK");
		}else{
			dbHandler.insert(chkVO.isAutoflag(), (int)(chkVO.getWaittime()/1000), chkVO.getViewtype());
			Log.i("N1", "insert..............................OK");
		}
		super.onPause();
	}
	
	@Override
	protected void onStart() {
		Log.i("N1", "Setup onStart");
		super.onStart();
	}
}
